package com.aracky.flowable.controller;

import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import com.aracky.flowable.model.dto.AcceptOrRejectDTO;
import com.aracky.flowable.model.dto.StartFlowableDTO;
import com.aracky.flowable.model.request.DoneRequest;
import com.aracky.flowable.model.request.TodoRequest;
import com.aracky.flowable.model.vo.DoneTaskVO;
import com.aracky.flowable.model.vo.HistoricActivityInstanceVO;
import com.aracky.flowable.model.vo.TodoTaskVO;
import com.aracky.flowable.service.FlowTaskService;
import com.aracky.flowable.service.ProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.aracky.flowable.constants.FlowableConstants.ACTIVITYTYPE.*;

/**
 * @Author zengzhiping
 * @BelongsPackage com.aracky.flowable.controller
 * @CreateTime 2022-08-17
 * @Description 通用任务接口
 */
@Api(tags = "通用任务相关接口")
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private ProcessService processService;

    @ApiOperation("启动任务")
    @PostMapping("start")
    public CommonResult<String> startFlowable(@RequestBody StartFlowableDTO dto) {
        return CommonResult.success(flowTaskService.startFlowable(dto), "启动成功");
    }


    @ApiOperation("批量启动任务")
    @PostMapping("batchStart")
    public CommonResult<String> batchStartFlowable(@RequestBody List<StartFlowableDTO> dtoList) {
        flowTaskService.batchStartFlowable(dtoList);
        return CommonResult.success("启动成功");
    }


    @GetMapping("getChart")
    @ApiOperation("流程图")
    public ResponseEntity<byte[]> getFlowChart(String processId) {
        return processService.genProcessDiagram(processId);
    }

    @ApiOperation("通过或者拒绝")
    @PostMapping("acceptOrReject")
    public CommonResult acceptOrReject(@RequestBody AcceptOrRejectDTO dto) {
        String orReject = flowTaskService.acceptOrReject(dto);
        return CommonResult.success(orReject);
    }

    @ApiOperation("用户待办")
    @PostMapping("todoPageList")
    public CommonResult<PageVO<TodoTaskVO>> todoList(@RequestBody TodoRequest todoRequest) {
        return CommonResult.success(flowTaskService.todoList(todoRequest), "查询成功");
    }

    @ApiOperation("用户已办")
    @PostMapping("donePageList")
    public CommonResult<PageVO<DoneTaskVO>> doneList(@RequestBody DoneRequest doneRequest) {
        return CommonResult.success(flowTaskService.doneList(doneRequest), "查询成功");
    }


    @ApiOperation(value = "获取指定用户组流程任务列表", hidden = true)
    @GetMapping("tasks-group")
    public CommonResult<List<TodoTaskVO>> tasks(String group) {
        List<Task> tasks = flowTaskService.tasks(group);
        List<TodoTaskVO> todoTaskVOS = ConvertUtil.sourceToTargets(tasks, TodoTaskVO.class);
        return CommonResult.success(todoTaskVOS, "查询成功");
    }


    @ApiOperation("查看当前任务的历史流程记录")
    @GetMapping("getHistoryInstances")
    public CommonResult<List<HistoricActivityInstanceVO>> historicActivityInstances(String processInstanceId) {
        List<HistoricActivityInstance> historicActivityInstances = flowTaskService.historicActivityInstances(processInstanceId);

        List<HistoricActivityInstanceVO> historicActivityInstanceVOList = ConvertUtil.sourceToTargets(historicActivityInstances, HistoricActivityInstanceVO.class);

        historicActivityInstanceVOList = historicActivityInstanceVOList.stream().filter(history -> {
            return !StringUtils.equalsAnyIgnoreCase(history.getActivityType(), SEQUENCEFLOW, STARTEVENT, ENDEVENT);
        }).collect(Collectors.toList());

        return CommonResult.success(historicActivityInstanceVOList, "查询成功");
    }

    @ApiOperation("驳回流程")
    @GetMapping("backtTask")
    public CommonResult currentTask(String taskId, String targetTaskKey) {
        String currentTask = flowTaskService.currentTask(taskId, targetTaskKey);
        return CommonResult.success(currentTask);
    }

}
